package com.softeem.idriveserver.controller;

import cn.hutool.core.util.RandomUtil;
import cn.hutool.crypto.SecureUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.softeem.idriveserver.dto.Result;
import com.softeem.idriveserver.entity.User;
import com.softeem.idriveserver.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Objects;

@RestController
public class UserController {
    @Autowired
    private UserService userService;

    //查询所有
    @GetMapping("/user/list")
    public List<User> list(@RequestParam(defaultValue = "1") Integer page,
                           @RequestParam(defaultValue = "5") Integer limit){
        //分页查询
        return userService.list(new Page<>(page,limit));
    }

    //用户登录
    @GetMapping("/user/login")
    public Result login(String phone,String password){
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("phone",phone);
        User user = userService.getOne(queryWrapper);
        if (user==null) return Result.fail().setMsg("用户不存在");
        password=password+user.getToken();
        password = SecureUtil.md5(password);
        if (user.getPassword().equals(password)) return Result.ok().setMsg("登录成功");
        else return Result.fail().setMsg("密码错误");
    }

    //用户注册
    @GetMapping("/user/registry")
    public Result registry(String phone,String password){
        //创建查询条件包装器
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        //设置查询条件
        queryWrapper.eq("phone",phone);
        User user = userService.getOne(queryWrapper);
        if (user!=null) return Result.fail().setMsg("该用户已存在");

        //对密码加盐加密
        //生成随机盐
        String token = RandomUtil.randomString(16);
        //将随机盐拼接到密码后面
        password = password + token;
        //md5加密
        password = SecureUtil.md5(password);
        user = new User();
        user.setPhone(phone);
        user.setPassword(password);
        user.setToken(token);

        return userService.save(user)?Result.ok().setMsg("注册成功"):Result.fail().setMsg("注册失败");
    }

    //信息修改
    @GetMapping("/user/modify")
    public Result modify(User user){
        if (user.getId() == null) return Result.fail().setMsg("请输入id");

        User oldUser = userService.getById(user.getId());
        if (Objects.isNull(oldUser)) return Result.fail().setMsg("用户不存在");

        if (Objects.nonNull(oldUser.getPassword())){
            if (Objects.equals(oldUser.getPassword(),SecureUtil.md5(user.getPassword()+oldUser.getToken()))){
                return Result.fail().setMsg("新旧密码相同");
            }

            String token = RandomUtil.randomString(16);
            String password = user.getPassword()+token;
            password = SecureUtil.md5(password);
            user.setPassword(password);
            user.setToken(token);
        }

        return userService.updateById(user)?Result.ok().setMsg("修改成功"):Result.fail().setMsg("修改失败");
    }

    //根据id查询用户
    public Result queryById(Integer id){

        return null;
    }

}
